/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package routeprovider;

import java.util.ArrayList;
import java.util.Collection;
import network.Base.PathElement;
import routeprovider.Algoritmes.Dijkstra;

/**
 *
 * @author Lega
 */
public class Search implements RouteProvider {

    static public enum searchType { cost, distance, time, elementsCount};

    // TODO: add throwing an exception if elements_ is null.
    public Search (Collection<PathElement> elements_) {
        elements = elements_;
    }

    // TODO: unfinished method. Add implimintation.
    public Collection<PathElement> getRoute(PathElement source,
            PathElement dest) {
        return null;
    }

    public double getCost(PathElement source,
            PathElement dest) {

        Dijkstra searchEngine = new Dijkstra (elements);
        return searchEngine.getCost(source, dest);
    }

    // TODO: unfinished method. Add implimintation.
    public String getDescription() {
        return null;
    }

    /**
     * command line arguments:
     *  load <filename> <modelname> load network model,
     *  delete <modelname> delete network model,
     *  list -models - print all accessible models,
     *  list -current print elements of current model,
     *  route -id id1 id2 - get route from element[id1] to element[id2],
     *  route -ip ip1 ip2 - get route from ip1 to ip2.
     * @param args
     */
    // TODO: unfinished method. Add implimintation.

    private Collection<PathElement> elements;

    private PathElement source;
    private PathElement dest;

    private class Routine {

        public int operationCode;
        public ArrayList<String> param;
        public ArrayList<String> options;

        public Routine (int opcode, ArrayList<String> param_,
                ArrayList<String> options_) {
            operationCode = opcode;
            param = (ArrayList<String>) param_.clone();
            options = (ArrayList<String>) options_.clone();
        }
    }

    private static final int load = 0x1;
    private static final int delete = 0x2;
    private static final int list = 0x3;
    private static final int route = 0x4;

    // TODO: 1. copy objects; 2. init an abstract class; 3. regexpo in java.
    private Routine getRoutine (String[] routine_) {
        if (routine_ == null) {
            throw new NullPointerException("routine string is null");
        }

        if (routine_.length < 2) {
            return null;
        }

        if (routine_[0].matches("load") && routine_.length == 3 ) {
            ArrayList<String> fname = new ArrayList<String>();
            fname.add(routine_[1]);
            fname.add(routine_[2]);
            return new Routine(load, fname, null);

        } else if (routine_[0].matches("delete") && routine_.length == 2) {
            ArrayList<String> fname = new ArrayList<String>();
            fname.add(routine_[1]);
            return new Routine(delete, fname, null);

        } else if (routine_[0].matches("list") && routine_.length == 2) {
            if (routine_[1].matches("-current") ||
                    routine_[1].matches("-models")) {
                ArrayList<String> options = new ArrayList<String>();
                options.add(routine_[1]);
                return new Routine(list, null, options);
            } else {
                return null;
            }
        } else if (routine_[0].matches("route") && routine_.length == 4) {
            if (!routine_[1].matches("-ip") && !routine_[1].matches("-id")) {
                return null;
            }
            ArrayList<String> param = new ArrayList<String>();
            ArrayList<String> options = new ArrayList<String>();
            param.add(routine_[2]);
            param.add(routine_[3]);
            options.add(routine_[1]);
            return new Routine(route, param, options);
        } else {
            return null;
        }
    }
}
